package com.databaseprotect;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.*;

public class Login extends MainActivity {
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        context=this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }
    public void login(View v){
        new Thread(new Runnable() {
            @Override
            public void run() {
                EditText editText_username=(EditText) findViewById(R.id.phone_num);
                EditText editText_password=(EditText)findViewById(R.id.psw);
                String username=editText_username.getText().toString();
                String t_password=editText_password.getText().toString();
                Connection conn = null;
                conn=(Connection)DBHelper.getConn();
                Statement statement=null;
                try {
                    statement=conn.createStatement();
                    String SQL="select * from user where username='"+username+"';";
                    System.out.println(SQL);
                    ResultSet resultSet=statement.executeQuery(SQL);
                    boolean change=false;
                    while (resultSet.next()){
                        String sql_password=resultSet.getString("password");
                        String user_type=resultSet.getString("type");
                        System.out.println(sql_password);
                        if(t_password.equals(sql_password)){
                            m_username=username;
                            m_user_type=user_type;
                            conn.close();
                            statement.close();
                            resultSet.close();
                            finish();
                            if (User_Status==true) {
                                User.instance.finish();
                            }
                            Intent intent=new Intent(context,MainActivity.class);
                            startActivity(intent);
                        }
                    }
                    if(change==false) {
                        Looper.prepare();
                        Toast.makeText(Login.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                        editText_username.setText("null");
                        editText_password.setText("null");
                        conn.close();
                        statement.close();
                        resultSet.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public void toRegister(View v){
        Intent intent=new Intent(this,Register.class);
        startActivity(intent);
    }
}
